package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    //dubbo注入service
    @Reference
    private CheckItemService checkItemService;

    /**
     * 新增检查项方法
     *
     * @param checkItem
     * @return
     */
    @RequestMapping("/add.do")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            int count = checkItemService.add(checkItem);
            if (count > 0) {
                return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
            } else {
                return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL, "网络繁忙，请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL, e.getMessage());
        }
    }

    //分页查询
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = checkItemService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true,"查询数据成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询数据失败","网络繁忙，请稍后再试");
        }
    }


    //删除检查项
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping("/deleteById/{id}.do")
    public Result deleteById(@PathVariable Integer id) {
        System.out.println("id = " + id);
        try {
            int count = checkItemService.deleteById(id);
            if (count < 0){
                return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL, "网络繁忙，请稍后再试");
            }
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL,e.getMessage());
        }
    }

    @RequestMapping("/findById/{id}.do")
    public Result findById(@PathVariable Integer id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true,"查询数据成功",checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询数据失败","网络繁忙，请稍后再试");
        }
    }

    //编辑检查项
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            int count = checkItemService.edit(checkItem);
            if (count > 0) {
                return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
            } else {
                return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL, "网络繁忙，请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
//            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL, e.getMessage());
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findAll.do")
    public Result findAll() {
        try {
            List<CheckItem> checkItemList = checkItemService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


}
